//
//  PrivacyConfirmView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 17.03.25.
//

import SwiftUI

struct PrivacyCheckView: View {
    
    @Binding var isChecked:Bool
    @State var isOpenPrivacyView:Bool = false
    @State var isOpenTermsView:Bool = false
    
    var body: some View {
        HStack {
            Toggle(isOn: $isChecked) {
                HStack(spacing:0) {
                    Text("我已阅读并同意")
                        .font(.system(size: 13))
                        .foregroundColor(Color.koiColorTextMain)
                    
                    Button (action:{
                        isOpenTermsView.toggle()
                    }){
                        Text("《用户协议》")
                            .font(.system(size: 13))
                            .foregroundColor(Color.koiColorPrivacyConfirmHighlightText)
                    }
                    
                    Text("和")
                        .font(.system(size: 13))
                        .foregroundColor(Color.koiColorTextMain)
                    
                    Button(action: {
                        isOpenPrivacyView.toggle()
                    }) {
                        Text("《隐私政策》")
                            .font(.system(size: 13))
                            .foregroundColor(Color.koiColorPrivacyConfirmHighlightText)
                    }
                }
                
            }.toggleStyle(CheckBoxStyle())
        }
        .fullScreenCover(isPresented: $isOpenPrivacyView, content: {PrivacyWebView.privacyView})
        .fullScreenCover(isPresented: $isOpenTermsView, content: {PrivacyWebView.termsView})
    }
}

struct CheckBoxStyle : ToggleStyle {
    func makeBody(configuration: Configuration) -> some View {
        Button(action:{
            configuration.isOn.toggle()
        }) {
            HStack {
                if (configuration.isOn) {
                    Image(systemName: "checkmark.circle.fill")
                        .renderingMode(.template)
                        .foregroundColor(Color.koiColorPrimary)
                } else {
                    Image(systemName: "checkmark.circle")
                        .renderingMode(.template)
                        .foregroundColor(Color.koiColorTitleSecond)
                }
                configuration.label
            }
        }
    }
}

@available(iOS 18.0, *)
#Preview {
    @Previewable @State var privacyChecked:Bool = false
    PrivacyCheckView(isChecked:$privacyChecked)
}
